
package com.idealighter.game.games.sirenniuniu.struct;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.dictionary.domain.SirenniuniuRoomDomain;
import com.idealighter.game.games.common.AbstractRoom;
import com.idealighter.game.games.common.AbstractSeat;
import com.idealighter.game.games.common.AbstractTable;
import com.idealighter.game.games.common.InstanceState;
import com.idealighter.game.games.sirenniuniu.manager.SiRenNiuNiuDataMgr;
import com.idealighter.game.server.context.ApplicationContext;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 四人牛牛房间 .
 * 
 * @date 2016年1月20日 下午5:08:17
 *
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class SiRenNiuNiuRoom extends AbstractRoom {

  private final int instanceId;

  private InstanceState instanceState;

  private SirenniuniuRoomDomain roomDomain;

  // 桌子
  private final Map<Integer, SiRenNiuNiuTable> tables;

  /**
   * 构造函数 .
   * 
   * @param roomDom .
   */
  public SiRenNiuNiuRoom(int instanceId, SirenniuniuRoomDomain roomDom) {
    super(roomDom.getId(), roomDom.getName());
    this.instanceState = InstanceState.NORMAL;
    this.instanceId = instanceId;
    this.roomDomain = roomDom;
    this.tables = createTables(roomDom.getTable());
  }

  /**
   * 创建桌子 .
   * 
   * @param tableNum .
   * @return
   */
  private Map<Integer, SiRenNiuNiuTable> createTables(int tableNum) {
    Map<Integer, SiRenNiuNiuTable> tables = new LinkedHashMap<>();

    for (int i = 0; i < tableNum; i++) {
      int tableId = instanceId * AbstractTable.MAX_TABLE + i + 1;
      tables.put(tableId, new SiRenNiuNiuTable(tableId, this));
    }

    return tables;
  }

  public SiRenNiuNiuTable removeTable(int tableId) {
    return tables.remove(tableId);
  }

  @Override
  public Game game() {

    return Game.SIREN_NIUNIU;
  }

  @Override
  public AbstractSeat seat(long playerId) {

    return ApplicationContext.getBean(SiRenNiuNiuDataMgr.class).getPlayerSeat(playerId);
  }

  @Override
  public Collection<SiRenNiuNiuTable> tables() {

    return tables.values();
  }

}
